﻿namespace WMS6.Web.Inbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundServiceRef;
    using WMS6.Web.UserControls;

    public class ReceivingList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F4022;
        protected GridView gvReceiving;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiving;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2ReceiptList(ReceiptQueryEntity receiptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(receiptQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = InboundServiceClientProxy.GetInstance().GetReceiptInfoByQueryEntity(receiptQuery);
            base.BindData2GridView(this.gvReceiving, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvReceiving, "RECEIPT_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4200_012");
                }
                else
                {
                    this.lblMsg.Text = InboundServiceClientProxy.GetInstance().DeleteReceiptInfo(selectedID) + base.GetGlobalMsgResource("F4200_026");
                    ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
                    this.BindData2ReceiptList(receiptQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4200_027") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvReceiving, "RECEIPT_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4200_012");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4200_025");
            }
            else
            {
                base.Response.Redirect(string.Format("Receiving.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = receiptQuery.CurrentPage;
            receiptQuery.CurrentPage = "0";
            this.gvReceiving.Columns[1].Visible = false;
            this.gvReceiving.AllowSorting = false;
            this.BindData2ReceiptList(receiptQuery);
            this.hdnActionID.Value = "";
            this.gvReceiving.ShowFooter = false;
            receiptQuery.IsGetAll = false;
            receiptQuery.CurrentPage = currentPageIndex;
            this.SetReceiptQueryEntity(receiptQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Receiving.xls", this.gvReceiving);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.CurrentPage = "0";
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            receiptQuery.ReceiptID = "";
            receiptQuery.OwnerID = "";
            receiptQuery.ExternalReceiptID = "";
            receiptQuery.Status = "";
            receiptQuery.CarrierID = "";
            receiptQuery.ReceiptType = "";
            receiptQuery.VendorID = "";
            receiptQuery.VendorName = "";
            receiptQuery.PoID = "";
            this.BindData2ReceiptList(receiptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            if (int.Parse(receiptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                receiptQuery.CurrentPage = (int.Parse(receiptQuery.CurrentPage) + 1).ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
            if (int.Parse(receiptQuery.CurrentPage) > 0)
            {
                receiptQuery.CurrentPage = (int.Parse(receiptQuery.CurrentPage) - 1).ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        private ReceiptQueryEntity GetReceiptQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as ReceiptQueryEntity);
            }
            return (base.PageQueryEntitySession as ReceiptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
                receiptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2ReceiptList(receiptQuery);
            }
        }

        protected void gvReceiving_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F4022", this.gvReceiving);
        }

        protected void gvReceiving_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvReceiving_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ReceiptQueryEntity receiptQuery = this.GetReceiptQueryEntity();
                receiptQuery.SortField = e.SortExpression;
                if (receiptQuery.SortDirection == "ASC")
                {
                    receiptQuery.SortDirection = "DESC";
                }
                else if (receiptQuery.SortDirection == "DESC")
                {
                    receiptQuery.SortDirection = "ASC";
                }
                this.BindData2ReceiptList(receiptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ReceiptQueryEntity receiptQuery = new ReceiptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                receiptQuery.ReceiptID = this.PreviousPage.ASNID;
                receiptQuery.OwnerID = this.PreviousPage.OwnerID;
                receiptQuery.ExternalReceiptID = this.PreviousPage.ExternalASNID;
                receiptQuery.Status = this.PreviousPage.Status;
                receiptQuery.CarrierID = this.PreviousPage.CarrierID;
                receiptQuery.ReceiptType = this.PreviousPage.ReceiptType;
                receiptQuery.VendorID = this.PreviousPage.VendorID;
                receiptQuery.VendorName = this.PreviousPage.VendorName;
                receiptQuery.PoID = this.PreviousPage.PoID;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            receiptQuery.IsGetAll = false;
            receiptQuery.CurrentPage = "0";
            receiptQuery.PageSize = this.gvReceiving.PageSize.ToString();
            receiptQuery.SortField = "RECEIPT_ID";
            receiptQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvReceiving.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetReceiptQueryEntity(receiptQuery);
            this.BindData2ReceiptList(receiptQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetReceiptQueryEntity(ReceiptQueryEntity receiptQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = receiptQuery;
            }
            else
            {
                base.PageQueryEntitySession = receiptQuery;
            }
        }

        public ReceivingQuery PreviousPage
        {
            get
            {
                return (ReceivingQuery) base.PreviousPage;
            }
        }
    }
}

